package com.web.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.web.annotation.CurrentUser;
import com.web.domain.Response;
import com.web.entity.*;
import com.web.service.EnterpriseService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping(value = "/app")
public class EnterpriseController {
    @Autowired
    private EnterpriseService enterpriseService;

    /**
     * 企业详情
     * @param info
     * @return
     */
    @PostMapping("/Enterprise/getEnterpriseDetails")
    public Response getEnterpriseDetails(@CurrentUser UserInfo info,Integer id){
        if(id == null){
            return Response.custom().failure("缺少必要参数！");
        }
        EnterpriseDetails enterpriseDetails = enterpriseService.getEnterpriseDetails(id);

        return Response.custom().ok().addParams(enterpriseDetails);
    }

    /**
     * 企业详情
     * @param info
     * @param id
     * @param type
     * @param pageNum
     * @return
     */
    @PostMapping("/Enterprise/EnterpriseTypeDetails")
    public Response EnterpriseTypeDetails(@CurrentUser UserInfo info,Integer id,Integer type,Integer pageNum) throws IOException {
        if(id == null){
            return Response.custom().failure("缺少必要参数id！");
        }
        if(type == null){
            return Response.custom().failure("缺少必要参数type！");
        }
        if(pageNum == null){
            return Response.custom().failure("缺少必要参数pageNum！");
        }
        if(type==1){
            String device_photos = enterpriseService.findEnterpriseDevicePhotos(id);
            return Response.custom().ok().addParams(device_photos);
        }else if(type==2){

            Map<String,Object> map = new HashedMap();
            //需求
            map.put("Demand",enterpriseService.getEnteriseDemand(id,pageNum));
            //闲置
            map.put("Idle",enterpriseService.getEnteriseIdle(id,pageNum));
            //供应
            map.put("Supply",enterpriseService.getEnteriseSupply(id,pageNum));
            //招聘
            map.put("Recriitment",enterpriseService.getEnterpriseRecruitment(id,pageNum));
            //动态
            map.put("InstantLife",enterpriseService.getEnterpriseInstantLife(id,pageNum));
            //提问
            map.put("Quiz",enterpriseService.getEnterpriseQuiz(id,pageNum));
            //临时工
            map.put("TemporaryWork",enterpriseService.getEnterpriseTemporaryWork(id,pageNum));

            return Response.custom().ok().addParams(map);
        }
        else if(type==3){
            PageHelper.startPage(pageNum,10);
            List<Case> list = enterpriseService.getEnterpriseCase(id);
            PageInfo pageInfo = new PageInfo(list,10);
            if(pageNum > pageInfo.getPages()){
                return Response.custom().ok().addParams(new List[0]);
            }else{
                return Response.custom().ok().addParams(pageInfo.getList());
            }
        }else if(type==4){
            return Response.custom().failure("暂无添加！");
        }

        return Response.custom().failure("系统异常请联系管理员！");
    }




}
